﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ProductReleasing : System.Web.UI.Page
{
    private string uploadDir; //uploadDir变量存放文件保存路径
    string str = System.Guid.NewGuid().ToString("N");//构造唯一一个标识码
    MyDataDataContext db = new MyDataDataContext();
   
    protected void Page_Load(object sender, EventArgs e)
    {
        //默认将文件保存到网站根文件夹下的image子文件夹中
        uploadDir = Path.Combine(Request.PhysicalApplicationPath, "image");

        //页面首次加载
        if (!IsPostBack)
        {
            //获取商品分类信息
            var results = from r in db.GoodsType
                          select new
                          {
                              r.GoodsTypeName
                          };//映射需要显示的字段

            //设置DropDownList控件的数据源
            Type.DataSource = results;
            //调用DataBind()方法显示数据
            Type.DataBind();
        }
    }


    protected void OpenImage(object sender, EventArgs e) {
        //上传图片
        if (fupImage.PostedFile.FileName == "")//无文件上传
        {
            lblMsg.Text = "无文件上传！";
        }
        else //有文件上传
        {
            if (fupImage.PostedFile.ContentLength > 104857600)//文件大小超过200KB
            {
                lblMsg.Text = "文件大小不能超过100MB！";
            }
            else //文件大小未超过200KB
            {
                string extension = Path.GetExtension(fupImage.PostedFile.FileName);
                switch (extension.ToLower()) //判断文件类型
                {
                    case ".png":
                    case ".bmp":
                    case ".jpeg":
                    case ".jpg":
                    case ".gif":
                        break;
                    default:         //文件扩展名不是png、bmp、jpg、jpeg
                        lblMsg.Text = "文件扩展名必须是png、bmp、jpg或jpeg！";
                        return;
                }

                //获取上传文件的文件名
                string fileName = Path.GetFileName(fupImage.PostedFile.FileName);
                //上传的文件将以原文件名保存到网站根目录文件夹下的image子文件夹中
                string fullPath = Path.Combine(uploadDir, fileName);

                try
                {
                    fupImage.PostedFile.SaveAs(fullPath); //上传文件
                    Image1.ImageUrl = "image/" + fileName;//显示商品的新图片

                    //获取要上传物品的记录
                    SaleOfGoods goods = (from r in db.SaleOfGoods
                                         where r.GoodsID == str
                                         select r).First();

                    //图片路径保存至数据库
                    goods.GoodsPicture = "image/" + fileName;
                    //提交
                    db.SubmitChanges();
                    lblMsg.Text = "图片更换成功！";
                }
                catch (Exception ee)
                {
                    lblMsg.Text = ee.Message; //上传文件失败，显示出错信息
                }
            }
        }

    }

    //发布商品
    protected void SaveButton_Click(object sender, EventArgs e)
    {

        if (Session["UserId"] == null)//用户未登录，返回登录界面
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            //控件全部通过验证
            if (Page.IsValid)
            {
                //添加发布物品记录
                GoodsType typeID = (from r in db.GoodsType
                                    where r.GoodsTypeName == Type.SelectedValue
                                    select r).First();

                
                string LsTime = DateTime.Now.ToString();

                SaleOfGoods goods = new SaleOfGoods();
                goods.GoodsID = str;
                //商品名称
                goods.GoodsName = GoodsName.Text.Trim();
                //商品描述
                goods.GoodsDescription = GoodsDescription.Text.Trim();
                //商品价格
                goods.Price = double.Parse(Price.Text.Trim());
                goods.ReleaseTime =LsTime;
                goods.OffShelfTime = LsTime;
                goods.StateOfGoods = 1;
                goods.UserID = Session["UserId"].ToString();
                goods.GoodsPicture = Image1.ImageUrl;
               
                //商品类型
                //商品类型ID
                goods.GoodsTypeID = typeID.GoodsTypeID;
                goods.Type = typeID.GoodsTypeName;
                goods.GoodsOrigin = 1;//1表示商品来自于出售物品，2表示物品来自于想买物品

                //提交
                db.SaleOfGoods.InsertOnSubmit(goods);
                db.SubmitChanges();
                lblMsg.Text = "发布成功";
                //返回信息列表页面
                //Response.Redirect("ProductReleasing.aspx");
            }
        }  
    }

    //删除商品
    protected void DeleteButton_Click(object sender, EventArgs e)
    {
        //不需要检查控件验证是否通过
        //获取要删除的对象
        MyDataDataContext db = new MyDataDataContext();
        SaleOfGoods DeleteObj = (from r in db.SaleOfGoods
                                 where r.GoodsID == Request.QueryString["GoodsID"]
                                 select r).FirstOrDefault();

        //删除
        db.SaleOfGoods.DeleteOnSubmit(DeleteObj);
        db.SubmitChanges();

        //返回信息列表页面
        Response.Redirect("ChangeInformation.aspx");
    }

}